<template>
  <div>
    <van-nav-bar title="个人信息" class="page-nav-bar">
      <van-icon slot="left" name="cross" @click="$router.back()" />
    </van-nav-bar>
    <van-cell title="头像" is-link @click="$refs.fileRef.click()">
      <van-image
        round
        fit="cover"
        class="avatar"
        :src="user.photo"
      />
    </van-cell>
    <input ref="fileRef" type="file" hidden
    @change="onChangeFile"/>
    <!-- <img :src="imgPath"/> -->
    <van-cell title="昵称" is-link :value="user.name" @click="isUpdateNameShow = true"/>
    <van-cell title="性别" is-link :value="user.gender === 1?'女':'男'" @click="isUpdateGenderShow = true"/>
    <van-cell title="生日" is-link :value="user.birthday" @click="isUpdateBirthdayShow = true"/>

    <van-popup v-model="isUpdateNameShow" position="bottom" :style="{ height: '100%' }" >
      <!-- 重置数据方式2-通过v-if销毁组件 -->
      <update-name v-if="isUpdateNameShow" v-model="user.name" @close="isUpdateNameShow = false"></update-name>
    </van-popup>

    <van-popup v-model="isUpdateGenderShow" position="bottom">
      <update-gender v-if="isUpdateGenderShow" v-model="user.gender" @close="isUpdateGenderShow = false"></update-gender>
    </van-popup>

    <van-popup v-model="isUpdateBirthdayShow" position="bottom">
      <update-birthday v-if="isUpdateBirthdayShow" v-model="user.birthday" @close="isUpdateBirthdayShow = false"></update-birthday>
    </van-popup>

    <van-popup v-model="isUpdatePhotoShow" position="bottom" :style="{ height: '100%' }" >
      <update-photo v-if="isUpdatePhotoShow" :img-path="imgPath"
      @input="user.photo = $event"
      @close="isUpdatePhotoShow = false"></update-photo>
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import UpdateName from './components/update-name.vue'
import UpdateGender from './components/update-gender.vue'
import UpdateBirthday from './components/update-birthday.vue'
import UpdatePhoto from './components/update-photo.vue'
export default {
  name: 'UserPage',
  props: {
  },
  data () {
    return {
      user: {},
      isUpdateNameShow: false,
      isUpdateGenderShow: false,
      isUpdateBirthdayShow: false,
      isUpdatePhotoShow: false,
      imgPath: undefined
    }
  },
  components: { UpdateName, UpdateGender, UpdateBirthday, UpdatePhoto },
  async created () {
    const res = await getUserProfile()
    this.user = res.data.data
  },
  mounted () {},
  methods: {
    onChangeFile (e) {
      this.imgPath = URL.createObjectURL(e.target.files[0])
      this.isUpdatePhotoShow = true
    }
  }
}
</script>

<style lang="less" scoped>
.avatar {
  width: 60px;
  height: 60px;
}
.van-popup{
  background-color: #f5f7f9;
}
</style>
